var app = getApp()
var WxParse = require('../../wxParse/wxParse.js')
var https = require('../../utils/https.js')


const order = ['red', 'yellow', 'blue', 'green', 'red']

Page({
    data: {
        // text:"这是一个页面"
        list: [],
        windowHeight: 0,//获取屏幕高度
        indicatorDots: true,
        autoplay: true,
        interval: 5000,
        duration: 500,
        toView: "red",
        keyword: "",
        dotCurrent:0},
    upper(e) {
        console.log(e)
    },
    lower(e) {
        console.log(e)
    },
    scroll(e) {
      
      console.log(e);
    },
    tap(e) {
        for (let i = 0; i < order.length; ++i) {
            if (order[i] === this.data.toView) {
                this.setData({
                    toView: order[i + 1]
                })
                break
            }
        }
    },
    tapMove(e) {
        this.setData({
            scrollTop: this.data.scrollTop + 10
        })
    },
    onLoad: function (options) {

        var that = this;

        //加载首页小说数据
      https.get(app.globalData.domain+"/api/home/book",
            function (res) {
                var list = res.data;
                var finelist = [];
                var hotlist = [];
                for(let i=0 ; i<list.length; i++){
                    let book = list[i];
                    if(book.picUrl.substring(0,4) != 'http'){
                        
                        book.picUrl = app.globalData.domain+book.picUrl;
                    }
                    if(book.type == 4){
                        finelist[finelist.length]=book;
                    }
                    if(book.type == 3){
                        hotlist[hotlist.length]=book;
                                               
                    }
                    
                    
                    for (let i = 0; i < hotlist.length; i++) {
                        WxParse.wxParse('bookDesc' + i, 'html', hotlist[i].bookDesc, that);
                        if (i === hotlist.length - 1) {
                          WxParse.wxParseTemArray("bookDescArray",'bookDesc', hotlist.length, that)
                        }
                      }
                }
                that.setData({
                    hotlist : hotlist,
                    finelist : finelist
                });
            }, function (res) {
                console.info(res);
            });

      https.get(app.globalData.domain+"/api/home/listUpdateRank",
            function (res) {
                
                that.setData({
                    
                    newlist : res.data
                });
            }, function (res) {
                console.info(res);
            });

       
        //获取屏幕高度
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    windowHeight: res.windowHeight
                })
            }
        })
        
    },
    toList: function (e) {
      var that = this;

        
      var catId = e.currentTarget.dataset.cat ? e.currentTarget.dataset.cat:''; 
      var sortBy = e.currentTarget.dataset.sortby ? e.currentTarget.dataset.sortby:''; 
      var status = e.currentTarget.dataset.status ? e.currentTarget.dataset.status:''; 
      var keyword = that.data.keyword ? that.data.keyword:'';
      var title = e.currentTarget.dataset.title; 
        wx.navigateTo({
          url: `../list/list?keyword=${keyword}&title=${e.currentTarget.dataset.title}&catId=${catId}&sortBy=${sortBy}&status=${status}`
        });
      if (keyword){
        that.data.keyword = '';
        }
    },
    //点击事件处理
    clickDetail: function (e) {
        wx.navigateTo({
          url: `../detail/detail?id=${e.currentTarget.dataset.id}&category=${e.currentTarget.dataset.category}&title=${e.currentTarget.dataset.title}`
        });
    },
    onReady: function () {
        // 页面渲染完成
    },
    onShow: function () {
        // 页面显示
    },
    onHide: function () {
        // 页面隐藏
    },
    onUnload: function () {
        // 页面关闭
    },
    //点击事件处理
    bindViewTap: function (e) {
        console.log(e.currentTarget.dataset.id);
    },
    recchange: function(e) {
        this.setData({
            dotCurrent:e.detail.current
        });
    },
    onShareAppMessage: function (res) {
        return {
            title: '',
            path: 'pages/index/index',
            imageUrl:'',
            success: function (shareTickets) {
                console.info(shareTickets + '成功');
                // 转发成功
            },
            fail: function (res) {
                console.log(res + '失败');
                // 转发失败
            },
            complete:function(res){
                // 不管成功失败都会执行
            }
        }
  },
  keywordBind: function (e) {
    var that = this;
    that.setData({
      keyword: e.detail.value
    })
  },
  submit: function (e) {
    var that = this;
    console.log(that.data.keyword);
  }
    
})